'use strict'

import { Injectable } from '@angular/core'
import { Http } from '@angular/http'
import { observable } from 'mobx'
import 'rxjs/add/operator/map'

@Injectable()
export class OrderStore {
  @observable total = 0
  @observable orders = []

  @observable isLoading = true


  load$

  constructor(private http: Http) {
    console.log('constructor order store')
  }

  loadOrders() {
    if (this.load$) {
      this.load$.unsubscribe()
    }
    this.isLoading = true
    this.load$ = this.http.get('http://localhost:3000/api/v3/orders')
      .map(res => res.json())
      .subscribe(data => {
        console.log(data)
        this.total = data.total
        this.orders = data.data
        this.isLoading = false
      })
  }
}
